import { Injectable } from '@nestjs/common';
import * as crypto from 'crypto-js';

@Injectable()
export class ToolUtil {
  // 生成随机码
  randomStr(len = 4) {
    const varStr =
      '1234567890abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let text = '';
    for (let index = 0; index < len; index++) {
      text += varStr[Number.parseInt(Math.random() * varStr.length + '')];
    }
    return text;
  }

  // 加盐
  addSalt(text = '', salt = undefined) {
    if (typeof salt === 'undefined') {
      salt = this.randomStr(8);
    }
    text = crypto.MD5(text + salt).toString();
    return { salt, text };
  }
}
